<?php
namespace app\admin\controller\v1;

use app\common\model\Product as ProductModel;

use app\common\model\ProductPiece as ProductPieceModel;
use app\lib\exception\BaseException;
use think\Controller;
use think\Db;

class Product extends Controller
{
    public function getById($id)
    {
        $product = ProductModel::getById($id);
        return successReturn($product);
    }

    public function getAll()
    {
        $where = input('get.');
        $products = ProductModel::getAll($where);
        $products = collection($products)->toArray();
        $pre_url = config('common.domain').'/product/index/product_id/';
        foreach ($products as $k=>$v)
        {
            $products[$k]['share_url'] = $pre_url.$v['id'];
        }
        return successReturn($products);
    }

    public function createOne()
    {
        //验证产品参数是否齐全
        $data = input('post.');
        //处理上传文件
        $file = request()->file('img');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $save_path = '/' . 'uploads';//相对根目录路径
        if ($file) {
            $info = $file->move(ROOT_PATH . '/' . 'public' . $save_path);
            if (!$info) {
                // 上传失败获取错误信息
                $error = $file->getError();
                throw new BaseException(['msg' => $error]);
            }
            $data['img_url'] = str_replace("\\","/",$save_path . '/' . $info->getSavename());
        }
        $data['start_time'] = strtotime($data['start_date'] . ' 0:0:0');
        $data['end_time'] = strtotime($data['end_date'] . ' 23:59:59');
        $res = ProductModel::createOne($data);
        return successReturn($res);
    }


    public function updateOne($id)
    {

        $data = input('post.');
        unset($data['img_url']);
        unset($data['start_time']);
        unset($data['end_time']);
        unset($data['create_time']);
        unset($data['update_time']);
        $file = request()->file('img');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $save_path = '/' . 'uploads';//相对根目录路径
        if ($file) {
            $info = $file->move(ROOT_PATH . '/' . 'public' . $save_path);
            if (!$info) {
                // 上传失败获取错误信息
                $error = $file->getError();
                throw new BaseException(['msg' => $error]);
            }
            //防止linux不支持反斜杠，强转一次
            $data['img_url'] = str_replace("\\","/",$save_path . '/' . $info->getSavename());
        }
        $res = ProductModel::updateOne($id, $data);
        return successReturn($res);
    }

    public function deleteOne($id)
    {
        ProductModel::deleteOne($id);
        return successReturn();
    }

    public function updatePiece($id)
    {
        // 获取表单上传文件
        $files = request()->file('files');
        $save_path = '/' . 'public' . '/' . 'uploads';//相对根目录路径
        $i = 0;
        Db::startTrans();
        //删除原有的piece
        $pieceModel = new ProductPieceModel();
        $pieceModel->where('product_id=' . $id)->delete();
        foreach ($files as $file) {
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . '/' . 'public' . $save_path);
            if ($info) {
                // 成功上传后 获取上传信息
                //创建碎片
                $piece = [
                    'product_id' => $id,
                    'sort' => $i,
                    //防止linux不支持反斜杠，强转一次
                    'img_url' => str_replace("\\","/",$save_path . '/' . $info->getSavename()),
                ];
                $pieceModel::createOne($piece);
            } else {
                Db::rollback();
                // 上传失败获取错误信息
                throw (new BaseException(['msg' => $file->getError()]));
            }
            $i++;
        }
        Db::commit();
        return successReturn();
    }
}
